<?php

Class _Cvest extends Connect{
    
    private $cvest;
    private $all = array();
    private $level;
    
    function __construct(){
        $this->cvest = new Cvest();
    }
    
    function setLevel($level){
        $this->level = $level;
    }
    
    function get ($user, $level){
        #return 
        #$c = $this->cvest->get($level);
        $this->level = $level;
        $c = $this->cvest->getAll();
        $this->all = Cvest::$all;
        #return $this->getCompleteTask($user);
        $a = array_keys($this->all);
        $a = implode(',', $a);
        $this->getCompleteTask($user, $a);
               
        usort($this->all, array( "_Cvest", "cvestSort") );                
        return  $this->all; ;      
    }
    
    private function getCompleteTask( $user, $ids ){        
        $cvest['task'] = array();
        $sql = "SELECT * FROM `".PREFIX_TABLE."cvest` WHERE  `user` = '".$user."' AND `idCvest` in (". $ids .") ";       
        if(!$result = DBConnect::query($sql)){
            $this->_error(mysql_error());
            return false;
        }       
        while($arr = DBConnect::getOne($result)){
            if( isset( $this->all[$arr['idCvest']]['task'][$arr['idTask']] ) ){
                $this->all[$arr['idCvest']]['task'][$arr['idTask']]['count'] =  $arr['count'];       
                $this->all[$arr['idCvest']]['task'][$arr['idTask']]['inDb'] =  1;
            }            
        }
    }
    
    private function searchById( int $id, $cvests){
        foreach($cvests as $k => $v){
            if(intval($v['id']) == $id ){
                return $v;
            }
        }
        return false;
    }
    
    function cvestSort($a, $b){
        if(intval($a['level']) == intval($b['level']) ){
            return  0; 
        }
        return (intval($a['level']) < intval($b['level']) )? -1: 1;
    }
    
    function runTask($user, $action, $type, $good, $count){
        #return array($action, $type, $good, $count);
        $update = false;               
        if( is_array($this->all) && count($this->all) > 0 ) foreach( $this->all as $key => $cvest){
            
            if( intval($cvest['level']) > $this->level ){                
                continue;    
            } 
            foreach($cvest['task'] as $key1 => $task){
                                
                if($task['count'] == 0) continue;
                if($task['action'] == $action && $task['type'] == $type && $task['good'] == $good ){
                    $task['count'] = max(($task['count'] - $count), 0);
                    $cvest['task'][$key1] = $task; 
                    $this->all[$key] = $cvest;
                    if( intval($task['inDb']) ==1 ){
                        $this->update($user, $task);    
                    }else{
                        $this->insert($user, $task, $cvest['id']);
                    }                    
                    #return $this->is_complete( $cvest );
                    if($this->is_complete( $cvest )){                                                
                        return array("m1" => $cvest['money1'],
                                     "m2" => $cvest['money2'],
                                     "p" => $cvest['practic']  );
                    }
                    return false;                       
                }
            }
        }
        return false;        
    }
        
    
    private function is_complete($cvest){
        $count = 0;
        if(is_array($cvest['task']) && count($cvest['task']) > 0) foreach($cvest['task'] as $k => $v){
            $count = $count + intval( $v['count'] );
        }
        return ($count > 0) ? false : true ; 
    }
    
    private function update($user, $task){ 
        $sql = "UPDATE `".PREFIX_TABLE."cvest` SET 
                `count` = '".$task['count']."'
                WHERE `idTask` = '".$task['id']."' AND `user` = '".$user."'";
        if(!$result = DBConnect::query($sql)){
            $this->_error(mysql_error());
            return false;
        }        
        return true;
    }
    
    
    private function insert($user, $task, $idCvest){
        $sql = "INSERT INTO `".PREFIX_TABLE."cvest` (`user`, `idCvest`, `idTask`, `count`)
                VALUES ( '".$user."', '".$idCvest."', '".$task['id']."', '".intval( $task['count'] )."' ) ";        
        if(!$result = DBConnect::query($sql)){
            $this->_error(mysql_error());
            return false;
        }
        return true;
    } 
    
    
    private function _insert($user, $tasks, $idCvest){
        $sql = "INSERT INTO `".PREFIX_TABLE."cvest` (`user`, `idCvest`, `idTask`, `count`)
                VALUES 
                ";
        $i = 1;
        foreach($tasks as $key => $val){
            $sql .= "( '".$user."',  '".$idCvest."',  '".$val['id']."', '".$val['count']."' )" ;
            $sql .= ($i < count($tasks))? ", " : "" ;
            $i++;
        }
        if(!$result = DBConnect::query($sql)){
            $this->_error(mysql_error());
            return false;
        }
        return true;
    } 
    
    
    
    private function _getCompleteTask($user, $cvest ){
        if(isset( $_SESSION['myCvest'][$cvest['id']]) ){
            foreach($cvest['task'] as $k => $v){
                if(isset($_SESSION['myCvest'][$cvest['id']]['task'][$k])){
                    $cvest['task'][$k]['count'] = $_SESSION['myCvest'][$cvest['id']]['task'][$k]['count'];       
                }    
            }
            $_SESSION['myCvest'][$cvest['id']] = $cvest;
            return;                
        }
        
        $cvest['task'] = ( is_array($cvest['task']) )? $cvest['task'] : array();
        
        $sql = "SELECT * FROM `".PREFIX_TABLE."cvest` WHERE `idCvest` = '".$cvest['id']."' AND `user` = '".$user."' AND ( ";
        $i = 1;
        foreach($cvest['task'] as $k => $v){
            $sql .= " `idTask` = '". intval( $v['id'] )."' ";
            $sql .= ($i < count($cvest['task']))? ' OR ' : ' ' ;
            $i++;            
        }
        $sql .= ' ) ';
                

        if(!$result = DBConnect::query($sql)){
            $this->_error(mysql_error());
            return false;
        }
        if(DBConnect::mysqlCount($result) < count($cvest['task'])){
            if( $this->insert($user, $cvest['task'], $cvest['id']) ){
                $_SESSION['myCvest'][$cvest['id']] = $cvest ;
            }
            return;
        }
        $i = 0;        
        while($arr = DBConnect::getOne($result)){
            $_SESSION['myCvest'][$cvest['id']] = $cvest; 
            #$_SESSION['myCvest'][$cvest['id']]['task'][$arr['idTask']]['id'] = $arr['id'];           
            $_SESSION['myCvest'][$cvest['id']]['task'][$arr['idTask']]['count'] = $arr['count'];
            $i++ ;
        }
    } 
    
    function _runTask($user, $action, $type, $good, $count){
        #return array($action, $type, $good, $count);
        $update = false;    
        
        $myKvest =  $_SESSION['myCvest'];   

        if( is_array($myKvest) && count($myKvest) > 0 ) foreach( $myKvest as $key => $val){
            
            if( intval($val['level']) > $this->level ){                
                continue;    
            } 
            foreach($val['task'] as $key1 => $val1){
                                
                if($val1['count'] == 0) continue;
                if($val1['action'] == $action && 
                    $val1['type'] == $type &&
                    $val1['good'] == $good
                    ){
                    
                    $myKvest[$key]['task'][$key1]['count'] =  max(($val1['count'] - $count), 0)  ;
                    
                    #test
                    #return $myKvest[$key]['task'][$key1];
                    
                    $this->update($user, $myKvest[$key]['task'][$key1]);                    
                    
                    if($this->is_complete($myKvest[$key])){                        
                        $_SESSION['myCvest'] = $myKvest;
                        return array("m1" => $myKvest[$key]['money1'],
                                     "m2" => $myKvest[$key]['money2'],
                                     "p" => $myKvest[$key]['practic']  );
                    }                       
                }
            }
        }
        return $_SESSION['myCvest'] = $myKvest;
    }  
    
    
}



?>